import wtforms
from wtforms import StringField
from wtforms.validators import DataRequired, Length, Regexp

from .base import BaseFrom
from ..libs.redis_tool.lsRedis import redisUtils


class UserForm(BaseFrom):
    username = StringField(validators=[DataRequired(message="username不允许为空"), Length(min=5, max=18)])
    # password = StringField(validators=[DataRequired(message="password不允许为空"), Length(min=8, max=16), Regexp(
    #     r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!@#$%^&*()_\-+])[A-Za-z\d~!@#$%^&*()_\-+]{8,16}$')])
    password = StringField(validators=[DataRequired(message="password不允许为空"), Length(min=8, max=16)])


class RegisterForm(BaseFrom):
    username = StringField(validators=[DataRequired(message="username不允许为空"), Length(min=5, max=18)])
    nickname = StringField(validators=[DataRequired(message="nickname不允许为空"), Length(min=2, max=18)])
    password = StringField(validators=[DataRequired(message="password不允许为空"), Length(min=8, max=16), Regexp(
        r'[a-zA-Z0-9]')])

    # password = StringField(validators=[DataRequired(message="password不允许为空"), Length(min=8, max=16)])

    def validate_username(self, failed):
        username = self.username.data
        user = redisUtils().get_value(username)
        if user:
            raise wtforms.ValidationError("用户已存在")

    def validate_nickname(self, failed):
        nickname = self.nickname.data
        nick = redisUtils().get_value(nickname)
        if nick:
            raise wtforms.ValidationError("昵称已存在")
